package com.testes.umov.sync;

import java.util.Timer;
import java.util.TimerTask;

public class TimeOutController {

	private Timer timer;
	private TimerTask timerTask;
	
	private long timeOut;
	private boolean timeOutExceeded; 
	
	public TimeOutController(final long timeOut){
		this.timeOut = timeOut;
	}
	
	public void start() {
        this.timerTask = new TimerTask() {

        	private int secondsCount = 0;
            
        	public void run() {
            	if (secondsCount++ == timeOut) {
            		timeOutExceeded = true;
                }
            }
        };

        this.timer = new Timer();
        this.timer.schedule(this.timerTask, 0, 1000);
    }

	public void stop(){
		if(this.timer != null){
			this.timer.cancel();
			this.timer = null;
		}
		
		if(this.timerTask != null){
			this.timerTask.cancel();
			this.timerTask = null;
		}
	}
	
	public boolean wasTimeOutExceeded(){
		return this.timeOutExceeded;
	}

}

